/*
 * @Author: yangrongxin
 * @Date: 2021-05-24 14:29:16
 * @LastEditors: yangrongxin
 * @LastEditTime: 2021-08-19 08:58:52
 */
import { useState, useEffect } from 'react';
import Taro from '@tarojs/taro';
import type { getSystemInfoSync } from '@tarojs/taro';

/*
 * @Description: Taro中获取小程序安全边界的hook
 */
export default function useSafeArea() {
  const [safeTop, setSafeTop] = useState(0);
  const [safeBottom, setSafeBottom] = useState(0);
  const [systemInfo, setSystemInfo] = useState<any>({});
  useEffect(() => {
    const { safeArea, screenHeight, ...rest }: getSystemInfoSync.Result = Taro.getSystemInfoSync();
    const { top } = Taro.getMenuButtonBoundingClientRect();
    setSafeTop(top);
    setSafeBottom(screenHeight - safeArea.bottom);
    setSystemInfo(rest);
  }, []);

  return [safeTop, safeBottom, systemInfo];
}
